package Solution;

import java.util.*;

public class Solution {
    public int nthUglyNumber(int n) {
//        // 大顶堆
//        int[] factors = {2, 3, 5};
//        Set<Long> seen = new HashSet<Long>();
//        PriorityQueue<Long> heap = new PriorityQueue<Long>();
//        seen.add(1L);
//        heap.offer(1L);
//        int ugly = 0;
//        for (int i = 0; i < n; i++) {
//            long curr = heap.poll();
//            ugly = (int) curr;
//            for (int factor : factors) {
//                long next = curr * factor;
//                if (seen.add(next)) {
//                    heap.offer(next);
//                }
//            }
//        }
//        return ugly;
        // 动态规划
        int[] dp = new int[n + 1];
        dp[1] = 1;
        int p2 = 1, p3 = 1, p5 = 1;
        for (int i = 2; i <= n; i++) {
            int num2 = dp[p2] * 2, num3 = dp[p3] * 3, num5 = dp[p5] * 5;
            dp[i] = Math.min(Math.min(num2, num3), num5);
            if (dp[i] == num2) {
                p2++;
            }
            if (dp[i] == num3) {
                p3++;
            }
            if (dp[i] == num5) {
                p5++;
            }
        }
        return dp[n];
    }
    public int numSquares(int n) {
        // dp[i] 表示和为i的完全平方数的最小数量
        int[] dp = new int[n+1];
        dp[1] = 1;
        dp[0] = 0;
        for (int i = 2; i <= n; i++) {
            int min = Integer.MAX_VALUE;
            for (int j = 1; j*j <= i; j++) {
                min = Math.min(dp[i-j*j],min);
            }
            dp[i] = min + 1;
        }
        System.out.println(Arrays.toString(dp));
        return dp[n];
    }
    public int lengthOfLIS(int[] nums) {
        // 最长严格递增子序列的长度
        // dp[i]表示以第i个元素里面的最长严格递增子序列
        // j 表示 nums左边第一个小于等于 nums[i] 的数
        // dp[i] = dp[j] +1
//        int n = nums.length;
//        int[] dp = new int[n];
//        dp[0] = 1;
//        for (int i = 1; i <n ; i++) {
//            int j = i-1;
//            int max = 0;
//            while (j>=0) {
//                if(nums[j] < nums[i]) max = Math.max(max,dp[j]);
//                j--;
//            }
//            dp[i] = max + 1;
//        }
//        return Arrays.stream(dp).max().getAsInt();
        // d[i] 表示长度为i的上升子序列的末尾元素的最小值
        // len 代表当前上升子序列的长度
        // d[i] 是关于i 单调递增的
        // 如果 d[i] >= d[j] (j<i) 根据定义 如果d[i] < d[j] 那么d[i] 必定小于 d[j] 否则的话，就说明不存在长度为j的序列
        int n = nums.length;
        int[] dp = new int[n+1];
        dp[1] = nums[1];
        int len = 1;
        for (int i = 1; i < n; i++) {
            if(nums[i] > dp[len]) {
                dp[++len] = nums[i];
            } else {
                int l = 1, r = len, pos = 0; // 如果找不到说明所有的数都比 nums[i] 大，此时要更新 d[1]，所以这里将 pos 设为 0
                while (l <= r) {
                    int mid = (l + r) >> 1;
                    if (dp[mid] < nums[i]) {
                        pos = mid;
                        l = mid + 1;
                    } else {
                        r = mid - 1;
                    }
                }
                dp[pos + 1] = nums[i];
            }
        }
        return len;
    }
    public int maxProfit(int[] prices) {
        // dp[i][0] 表示第i天持有股票的最大利润
        // dp[i][1] 表示第i天不持有股票的最大利润
        // dp[i][0] = Math.max(dp[i-1][0],dp[i-2][1]-prices[i]);
        // dp[i][1] = Math.max(dp[i-1][1],dp[i-1][0]+prices[i]);
        int n = prices.length;
        int[][] dp = new int[n][2];
        dp[0][0] = -prices[0];
        for (int i = 1; i < n; i++) {
            if(i>=2) {
                dp[i][0] = Math.max(dp[i-1][0],dp[i-2][1]-prices[i]);
            } else {
                dp[i][0] = Math.max(dp[i-1][0],-prices[i]);
            }
            dp[i][1] = Math.max(dp[i-1][1],dp[i-1][0]+prices[i]);
        }
        return dp[n-1][1];
    }
    public int nthSuperUglyNumber(int n, int[] primes) {
        int[] dp = new int[n + 1];
        dp[1] = 1;
        int[] p = new int[primes.length];
        Arrays.fill(p,1);
        for (int i = 2; i <= n; i++) {
            for (int j = 0; j < primes.length; j++) {
                int numI = dp[p[j]*primes[j]];
                dp[i] = Math.min(dp[i],numI);
            }
            for (int j = 0; j < primes.length; j++) {
                if(dp[i] == primes[j]) {
                    p[j]++;
                }
            }
        }
        return dp[n];
    }
    static class Seed {
        List<Seed[]> parents;
        int index; // 种子编号
        int getMinTime; // 获得种子的最短时间
        boolean hased; // 是否拥有
        int growTime;

        public Seed(int val) {
            this.index = val;
            this.parents = new ArrayList<>();
            this.getMinTime = -1;
        }
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int N = scan.nextInt(); // 种子类型数量
        int M = scan.nextInt(); // 已经拥有的种子数量
        int K = scan.nextInt(); // 可以杂交的方案
        int T = scan.nextInt(); // 目标种子
        Seed[] seeds = new Seed[N+1];
        for (int i = 1; i <=N ; i++) {
            seeds[i] = new Seed(i);
            int growTime = scan.nextInt();
            seeds[i].growTime = growTime;
        }
        for (int i = 0; i < M; i++) {
            int hasedSeed = scan.nextInt();
            seeds[hasedSeed].hased = true;
        }
        for (int i = 0; i < K; i++) {
            int A = scan.nextInt();
            int B = scan.nextInt();
            int C = scan.nextInt();
            // A*B = C
            seeds[C].parents.add(new Seed[]{seeds[A],seeds[B]});
        }
        System.out.println(dfs(T,seeds));
        scan.close();

    }
    // 获取种子target的最短时间
    private static int dfs(int target,Seed[] seeds) {
        if(seeds[target].hased) return 0;
        Seed seed = seeds[target];
        int res = -1;
        for (Seed parent[]: seed.parents) {
            Seed father = parent[0];
            Seed mother = parent[1];
            int getFatherTime = 0;
            int getMotherTime = 0;
            if(!father.hased) {
                getFatherTime = dfs(father.index,seeds);
                father.hased = false;
            }
            if(!mother.hased) {
                getMotherTime = dfs(mother.index,seeds);
                mother.hased = false;
            }
            int zajiaoTime = Math.max(father.growTime,mother.growTime);
            int totalTime = Math.max(getFatherTime,getMotherTime) + zajiaoTime;
            if(res == -1) res = totalTime;
            res = Math.min(res,totalTime);
        }
        seed.hased = true;
        return res;
    }

}
